import { getToken } from '@/utils/storage'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/home/Home'),
    redirect: '/article',
    children: [
      { path: '/article', component: () => import('@/views/home/article/Article') },
      { path: '/quertion', component: () => import('@/views/home/question/Question') },
      { path: '/user', component: () => import('@/views/home/user/User') },
      { path: '/video', component: () => import('@/views/home/video/Video') }
    ]
  },
  { path: '/detail/:id', component: () => import('@/views/detail/Detail') },
  { path: '/login', component: () => import('@/views/login/Login') },
  { path: '/userchat', component: () => import('@/views/userchat/UserChat') },
  { path: '/userprofile', component: () => import('@/views/userprofile/UserProfile') }
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login', '/article']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token || whiteList.includes(to.path)) return next()
  next('/login')
})

export default router
